﻿Namespace Collections
    Public Class SortableCollection(Of T)
        Inherits GenericCollection(Of T)
        Implements ISortableCollection(Of T)

#Region "Private Fields"
        Private _SortConditions As IList(Of T)
#End Region

#Region "Properties"
        Protected Overridable ReadOnly Property SortConditions() As System.Collections.Generic.IList(Of SortCriteria(Of T)) Implements ISortableCollection(Of T).SortConditions
            Get
                Return _SortConditions
            End Get
        End Property
#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal isReadonly As Boolean, ByVal items As IEnumerable(Of T))
            MyBase.New(isReadonly)
            For Each Item As T In items
                MyBase.List.Add(Item)
            Next
        End Sub
#End Region

        Public Overridable Function SortBy(ByVal criteria As SortCriteria(Of T)) As ISortableCollection(Of T) Implements ISortableCollection(Of T).SortBy
            Me.SortConditions.Clear()
            Me.SortConditions.Add(criteria)
            ApplySort()
            Return Me
        End Function

        Public Overridable Function ThenSortBy(ByVal criteria As SortCriteria(Of T)) As ISortableCollection(Of T) Implements ISortableCollection(Of T).ThenSortBy
            'If Me.SortConditions.Count = 0 Then Throw New SortException("You must sort the collection with ""SortBy"" before you can add additional sort criteria")
            Me.SortConditions.Add(criteria)
            ApplySort()
            Return Me
        End Function

        Protected Overridable Function ApplySort()
            Throw New NotImplementedException
        End Function

    End Class
End Namespace